package com.zhf.servlet;

import com.zhf.bean.Grade;
import com.zhf.bean.Student;
import com.zhf.service.GradeService;
import com.zhf.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/25 17:47
 */
@WebServlet(name = "GetGradeServlet",urlPatterns = "/getGrade")
public class GetGradeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Object className = request.getParameter("className");
        System.out.println("查询班级："+className+" 的班级信息");
        //检查学生是否存在
        GradeService gradeService = new GradeService();
        Grade grade = gradeService.getGrade((String) className);
        if (grade!=null){
            //将查到的学生写入session返回
            session.setAttribute("graderesult",grade);
            response.sendRedirect("graderesultpage.jsp");
        }else {
            //服务器跳转request共享
            request.setAttribute("error","该班级不存在");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("getgrade.jsp");
            requestDispatcher.forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
